package com.linjiahao.nettyOne.handler.Impl;

import com.linjiahao.nettyOne.constant.HandlerServiceName;
import com.linjiahao.nettyOne.handler.AbstractHandle;
import com.linjiahao.nettyOne.handler.HandlerServiceFactory;
import com.linjiahao.nettyOne.server.SendMessage;
import com.linjiahao.nettyOne.service.ChatMessageService;
import com.linjiahao.servicebase.vo.NettyMessageVO;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.UnknownHostException;

/**
 * @ClassName SignleMessageHandlerImpl
 * @Description: 单聊处理器
 * @Author linjiahao
 * @Date 11/1/2022
 **/
@Service
@Slf4j
public class SignleMessageHandlerImpl implements AbstractHandle, InitializingBean {

    @Autowired
    private ChatMessageService chatMessageService;

    @Autowired
    private SendMessage sendMessage;

    @Override
    public String getServiceTypeName() {
        return HandlerServiceName.SINGLE_MESSAGE.getName();
    }

    @Override
    public Object handlerWebsocket(ChannelHandlerContext ctx, Object message) throws UnknownHostException {
        NettyMessageVO nettyMessageVO = (NettyMessageVO)message;
        //新增消息记录
        nettyMessageVO = chatMessageService.addNewMessage(nettyMessageVO);
        sendMessage.send(ctx,nettyMessageVO,Boolean.FALSE);
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        HandlerServiceFactory.register(getServiceTypeName(),this);
    }
}
